package com.young.ysepay.utils;

import com.young.ysepay.utils.apiresult.ApiResult;
import com.young.ysepay.utils.apiresult.ApiResultCode;
import org.slf4j.Logger;

import java.util.Map;
import java.util.UUID;

/**
 * @anthor  wenguoqiang
 * @date:   2019/01/16 12:09
 * @company 蒙泰互联网公司
 * @des     返回值通用封装
 **/
public class ReturnUtils {

    //记录异常信息
    public static Map getExceptionResult(Logger log,Exception e){
        String errId = "ERRID" + UUID.randomUUID();
        log.error(errId , e);
        String errMsg = "[" + errId + "][" + e.getMessage() + "]";
        return  createReturnResult(ApiResultCode.OPRFAILURE.code(),errMsg,false,"");
    }

    //通用日志记录
    public static String appendResultStr(Logger log,String responseStr,String methodName){
        //追加调用方法名，以便android logcat看日志调试
        String methodStr = ",\"client_method_name\":"+"\""+methodName+"\"";
        StringBuffer logStr = new StringBuffer();
        if(responseStr.length() > 0) {
            StringBuffer buf = new StringBuffer(responseStr);
            buf.insert(responseStr.length() - 1, methodStr);
            responseStr = buf.toString();
        }
        logStr.append("\nAPP POST 返回信息 : " + responseStr);
        log.debug(logStr.toString());
        return responseStr;
    }

    //检测参数为空
    public static Map paramIsNull(){
        return createReturnResult(ApiResultCode.PARAMNULL.code(),ApiResultCode.PARAMNULL.message(),false,"");
    }

    public static Map createReturnResult(Integer code,String message, Boolean isSuccess, String result){
        return ApiResult.failure(code, message, result);
//        Map returnMap = new HashMap();
//        returnMap.put("resultCode", Code);
//        returnMap.put("resultMessage", message);
//        returnMap.put("success", isSuccess );
//        returnMap.put("result", result);
//        return returnMap;
    }
}
